package cn.lixin.pet.service.impl;

import cn.lixin.basic.service.impl.BaseServiceImpl;
import cn.lixin.pet.domain.Pet;
import cn.lixin.pet.domain.PetDetail;
import cn.lixin.pet.mapper.PetDetailMapper;
import cn.lixin.pet.mapper.PetMapper;
import cn.lixin.pet.service.IPetService;
import cn.lixin.user.domain.User;
import cn.lixin.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        PetDetail petDetail = pet.getDetail();
        if (petDetail != null){
            petDetail.setPet_id(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public void onSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("onSaleTime",new Date());
        petMapper.onSale(map);
    }
    /**
     * 下架
     * @param ids
     */
    @Override
    public void offSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        map.put("offSaleTime",new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet queryById(Long id) {
        return petMapper.loadById(id);
    }

    /**
     * 领养 ==> : 1下架商品：state=0、更新下架时间
     *           2绑定用户：setUserId
     *
     * @param petId
     * @param logininfoId
     */
    @Override
    public void adopt(Long petId, Long logininfoId) {
        Pet pet = petMapper.loadById(petId);
        pet.setState(0);
        pet.setOffsaletime(new Date());
        User user = userMapper.loadByLogininfoId(logininfoId);
        pet.setUser_id(user.getId());
        petMapper.update(pet);

    }
}
